package com.lee.demo.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Spanned;
import android.view.View;

import com.lee.demo.R;
import com.lee.demo.base.BaseActivity;
import com.lee.demo.base.SwipeBackActivity;
import java.io.File;

import cn.lee.cplibrary.util.LogUtil;
import cn.lee.cplibrary.util.ObjectUtils;
import cn.lee.cplibrary.util.takephotos.PhotoUtil;
import cn.lee.cplibrary.util.timer.TimeUtils;
import cn.lee.cplibrary.widget.richet.bean.FontStyle;
import cn.lee.cplibrary.widget.richet.handle.CustomHtml;
import cn.lee.cplibrary.widget.richet.handle.RichEditImageGetter;
import cn.lee.cplibrary.widget.richet.view.FontStylePanel;
import cn.lee.cplibrary.widget.richet.view.RichEditText;

/**
 * 发布资讯：Demo--只做展示使用
 * Created by ChrisLee on 2021/3/26.
 */

public class InfoCenterPublishDemoActivity extends SwipeBackActivity implements FontStylePanel.OnFontPanelListener, RichEditText.OnSelectChangeListener, View.OnClickListener {
    FontStylePanel fontStylePanel;
    RichEditText richEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PhotoUtil.init(this, savedInstanceState);//初始化照相 -相册信息
    }

    @Override
    protected BaseActivity getSelfActivity() {
        return this;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_info_center_publish_demo;
    }

    @Override
    public String getPagerTitle() {
        return null;
    }

    @Override
    public String getPagerRight() {
        return null;
    }

    @Override
    public void initView() {
        fontStylePanel = (FontStylePanel) findViewById(R.id.fontStylePanel);
        richEditText = (RichEditText) findViewById(R.id.richEditText);
        fontStylePanel.setOnFontPanelListener(this);
        richEditText.setOnSelectChangeListener(this);
        findViewById(R.id.btn_html).setOnClickListener(this);
        findViewById(R.id.btn_txt).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_html:
                richTextToHTML(richEditText);
                break;
            case R.id.btn_txt:
                String txt = richTextToHTML(richEditText);
                htmlToRichText(richEditText, txt);
                break;
            default:
                break;
        }
    }

    @Override
    protected void initData() {

    }


    /**
     * 富文本转成成HTML
     **/

    private String richTextToHTML(RichEditText ret) {
        String html = CustomHtml.toHtml(ret.getEditableText(), CustomHtml.TO_HTML_PARAGRAPH_LINES_CONSECUTIVE);
        LogUtil.i("", "span===" + ret.getEditableText());
        LogUtil.i("", "转html=" + html);
        return html;
    }

    /**
     * HTML转成富文本
     *
     * @param html_content HTML
     */
    private CharSequence htmlToRichText(RichEditText ret, String html_content) {
        if (ObjectUtils.isEmpty(html_content)) {
            return "";
        }
        Spanned spanned = CustomHtml.fromHtml(html_content, CustomHtml.FROM_HTML_MODE_LEGACY, new RichEditImageGetter(this, ret), null);
        LogUtil.i("", "html===" + html_content);
        LogUtil.i("", "转span=" + spanned);
        return spanned;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if ((requestCode == PhotoUtil.REQUEST_CAPTURE || requestCode == PhotoUtil.REQUEST_PICK) && resultCode == Activity.RESULT_OK) {//拍照或者相册
            final File file = PhotoUtil.getImageFile(this, requestCode, resultCode, data, TimeUtils.getCurTime(TimeUtils.DATETIME_NUM_FORMAT));
            if (file != null) {
                uploadImg(file);
            }
        }
    }


    public void uploadImg(final File imageFile) {
//        richEditText.setImg(url);
        richEditText.setImgFile(imageFile.getAbsolutePath());


    }


    //fontStylePanel的监听方法
    //加粗
    @Override
    public void setBold(boolean isBold) {
        richEditText.setBold(isBold);
    }

    //    斜体
    @Override
    public void setItalic(boolean isItalic) {
        richEditText.setItalic(isItalic);
    }

    //下划线
    @Override
    public void setUnderline(boolean isUnderline) {
        richEditText.setUnderline(isUnderline);
    }

    //中划线
    @Override
    public void setStreak(boolean isStreak) {
        richEditText.setStreak(isStreak);
    }

    @Override
    public void insertImg() {//插入图片
        PhotoUtil.showPicChooseDialog(getSelfActivity(), null);
    }

    //字体大小
    @Override
    public void setFontSize(int size) {
        richEditText.setFontSize(size);
    }

    //颜色设置
    @Override
    public void setFontColor(int color) {
        richEditText.setFontColor(color);
    }

    /******richEditText的监听方法*****/

    /**
     * 样式改变
     */
    @Override
    public void onFontStyleChang(FontStyle fontStyle) {
        fontStylePanel.initFontStyle(fontStyle);
    }

    /**
     * 光标选中监听
     */
    @Override
    public void onSelect(int start, int end) {

    }


}

